1 | <!DOCTYPE html>
|
2 | <html lang="en">
|
3 | <head>
|
4 | <meta charset="utf-8">
|
5 | <title>JSDoc: Tutorial: Autoupdating Collections</title>
|
6 |
|
7 | <script src="scripts/prettify/prettify.js"> </script>
|
8 | <script src="scripts/prettify/lang-css.js"> </script>
|
9 | |
10 |
|
11 |
|
12 | <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
13 | <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
14 | </head>
|
15 |
|
16 | <body>
|
17 |
|
18 | <div id="main">
|
19 |
|
20 | <h1 class="page-title">Tutorial: Autoupdating Collections</h1>
|
21 |
|
22 | <section>
|
23 |
|
24 | <header>
|
25 |
|
26 |
|
27 | <h2>Autoupdating Collections</h2>
|
28 | </header>
|
29 |
|
30 | <article>
|
31 | <h1>The Autoupdate Feature for Collections</h1><p>Autoupdate can be enabled on a per-collection basis via the constructor option <code>autoupdate: true</code>. The feature requires <code>Object.observe</code> (currently implemented in Chrome 36+, io.js and Node.js 0.12+). If observers are not available, the option will be ignored.</p>
|
32 | <p>Autoupdate automatically calls <code>update(doc)</code> whenever a document is modified, which is necessary for index updates and dirty-marks (used to determine whether the DB has been modified and should be persisted).</p>
|
33 | <p>Enabling this feature basically means, that all manual <code>update</code> calls can be omitted.</p>
|
34 | <h2>Example</h2><pre class="prettyprint source lang-js"><code>var doc = collection.by("name", "John");
|
35 |
|
36 | doc.name = "Peter";
|
37 | doc.age = 32;
|
38 | doc.gender = "male";
|
39 |
|
40 | collection.update(doc); // This line can be safely removed.</code></pre><p>Autoupdate will call <code>update</code> at the end of the current event loop cycle and thus only calls <code>update</code> once, even when multiple changes were made.</p>
|
41 | <h2>Error handling</h2><p>There is one important difference between autoupdate and manual updates. If for example a document change violates a unique key constraint, <code>update</code> will synchronously throw an error which can be catched synchronously:</p>
|
42 | <pre class="prettyprint source lang-js"><code>var collection = db.addCollection("test", [
|
43 | unique: ["name"]
|
44 | ]);
|
45 |
|
46 | collection.insert({ name: "Peter" });
|
47 |
|
48 | var doc = collection.insert({ name: "Jack" });
|
49 | doc.name = "Peter";
|
50 |
|
51 | try {
|
52 | collection.update(doc);
|
53 | } catch(err) {
|
54 | doc.name = "Jack";
|
55 | }</code></pre><p>Since autoupdate calls <code>update</code> asynchronously, you cannot catch errors via <code>try-catch</code>. Instead you have to use event listeners:</p>
|
56 | <pre class="prettyprint source lang-js"><code>var collection = db.addCollection("test", [
|
57 | unique: ["name"],
|
58 | autoupdate: true
|
59 | ]);
|
60 |
|
61 | collection.insert({ name: "Peter" });
|
62 |
|
63 | var doc = collection.insert({ name: "Jack" });
|
64 | doc.name = "Peter";
|
65 |
|
66 | collection.on("error", function(errDoc) {
|
67 | if(errDoc === doc) {
|
68 | doc.name = "Jack";
|
69 | }
|
70 | });</code></pre><p>This can become quite tedious, so you should consider performing checks before updating documents instead.</p>
|
71 | </article>
|
72 |
|
73 | </section>
|
74 |
|
75 | </div>
|
76 |
|
77 | <nav>
|
78 | <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Collection.html">Collection</a></li><li><a href="DynamicView.html">DynamicView</a></li><li><a href="Loki.html">Loki</a></li><li><a href="LokiEventEmitter.html">LokiEventEmitter</a></li><li><a href="LokiFsAdapter.html">LokiFsAdapter</a></li><li><a href="LokiFsStructuredAdapter.html">LokiFsStructuredAdapter</a></li><li><a href="LokiIndexedAdapter.html">LokiIndexedAdapter</a></li><li><a href="LokiLocalStorageAdapter.html">LokiLocalStorageAdapter</a></li><li><a href="LokiMemoryAdapter.html">LokiMemoryAdapter</a></li><li><a href="LokiPartitioningAdapter.html">LokiPartitioningAdapter</a></li><li><a href="Resultset.html">Resultset</a></li></ul><h3>Tutorials</h3><ul><li><a href="tutorial-Autoupdating Collections.html">Autoupdating Collections</a></li><li><a href="tutorial-Changes API.html">Changes API</a></li><li><a href="tutorial-Collection Transforms.html">Collection Transforms</a></li><li><a href="tutorial-Indexing and Query performance.html">Indexing and Query performance</a></li><li><a href="tutorial-Loki Angular.html">Loki Angular</a></li><li><a href="tutorial-Persistence Adapters.html">Persistence Adapters</a></li><li><a href="tutorial-Query Examples.html">Query Examples</a></li></ul>
|
79 | </nav>
|
80 |
|
81 | <br class="clear">
|
82 |
|
83 | <footer>
|
84 | Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Sun Dec 18 2016 19:39:52 GMT-0500 (Eastern Standard Time)
|
85 | </footer>
|
86 |
|
87 | <script> prettyPrint(); </script>
|
88 | <script src="scripts/linenumber.js"> </script>
|
89 | </body>
|
90 | </html> |
\ | No newline at end of file |